package acs.bridge.dao;

import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Properties;

/**
 * acs.bridge.dao
 * User: KuznetsovStV
 * Date: 02.02.14
 * Time: 19:29
 */
public class Bm_jreportDAOImpl {
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    private JdbcTemplate jdbcTemplate;


    public Clob fnc_get_module_cdata(final String a_module_tag, final String a_module_cdata_tag, final String a_module_type_tag) {

        return ((Clob) jdbcTemplate.execute(
                new CallableStatementCreator() {
                    public CallableStatement createCallableStatement(Connection connection) throws SQLException {
                        CallableStatement callableStatement = connection.prepareCall("{? = call BRIDGE.FNC_GET_MODULE_CDATA(?,?,?)}");
                        callableStatement.registerOutParameter(1, Types.CLOB);
                        callableStatement.setString(2, a_module_tag);
                        callableStatement.setString(3, a_module_cdata_tag);
                        callableStatement.setString(4, a_module_type_tag);
                        return callableStatement;
                    }
                },
                new CallableStatementCallback() {
                    public Object doInCallableStatement(CallableStatement callableStatement)
                            throws SQLException, DataAccessException {
                        callableStatement.execute();
                        return callableStatement.getClob(1);
                    }


                }
        ));
    }


    public Properties fnc_get_param_field_by_id(Connection a_connection,final String a_group_tag, final String a_meta_tag, final BigDecimal a_id, final String a_filed_name, final String a_source) throws SQLException {

        Properties l_prop = new Properties();
        Connection connection = a_connection;

        // Создание объекта ARRAY.

        Object[] l_Record;

        StructDescriptor l_structDesc =
                StructDescriptor.createDescriptor("BRIDGE.T_PARAM", connection);
        ArrayDescriptor l_arrayDesc =
                ArrayDescriptor.createDescriptor("BRIDGE.TT_PARAM", connection);

        ARRAY l_array = new ARRAY(l_arrayDesc, connection, null);

        OracleCallableStatement cStatement = (OracleCallableStatement) connection.prepareCall("{? = call BRIDGE.FNC_PARAM_FIELD_BY_ID(?,?,?,?,?)}");


        cStatement.registerOutParameter(1,
                OracleTypes.ARRAY,
                "BRIDGE.TT_PARAM");

        cStatement.setString(2, a_group_tag);
        cStatement.setString(3, a_meta_tag);

        cStatement.setBigDecimal(4, a_id);
        cStatement.setString(5, a_filed_name);
        cStatement.setString(6, a_source);


        cStatement.executeUpdate();

        l_array =
                cStatement.getARRAY(1);
        // Получение данных
        Object[] l_Objects = (Object[]) l_array.getArray();

        for (int i = 0; i < l_Objects.length; i++) {
            l_Record = ((STRUCT) l_Objects[i]).getAttributes();
            if (l_Record[1] != null)
                l_prop.put((String) l_Record[0], (String) l_Record[1]);

        }
        return l_prop;
    }



}
